package org.jypj.dev.controller;

import org.jypj.dev.model.Template;
import org.jypj.dev.service.ITemplateService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenYu on 2017-05-26.
 * 出乘提示controller
 */
@Controller
@RequestMapping(value = "trainNoticeController")
public class TrainNoticeController {

    @Resource
    private ITemplateService templateService;

    /**
     * 跳转到出乘提示设置页面
     *
     * @return
     */
    @RequestMapping(value = "toTrainNotice.action")
    public String toTrainNotice() {

        return "back/message/train_notice";
    }

    /**
     * 查询所有模板
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectAllTemplate.action")
    public Map<String, Object> selectAllTemplate() {
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            List<Template> templates = templateService.selectAllByMap(queryMap);
            dataMap.put("tableData", templates);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

    /**
     * 移动模板
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "moveTemplate.action")
    public Map<String, Object> moveTemplate(String id1, String id2) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            int i = templateService.moveTemplate(id1, id2);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

    /**
     * 预览
     *
     * @return
     */
    @RequestMapping(value = "show")
    public String toTrainNoticeShow(HttpServletRequest request) {
        Map<String, Object> queryMap = new HashMap<>();
        List<Template> templates = templateService.selectAllByMap(queryMap);
        request.setAttribute("ts", templates);
        return "webApp/train_notice_show";
    }


    /**
     * 保存或者更新
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveOrUpdate.action")
    public Map<String, Object> saveOrUpdate(Template template) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            String todayString = DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss");
            template.setModifyTime(todayString);
            if (StringUtil.isNotEmpty(template.getId())) {
                templateService.updateTemplateSome(template);
            } else {
                template.setCreateTime(todayString);
                templateService.saveTemplate(template);
            }
            dataMap.put("template", template);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }


    /**
     * 按ID删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteTemplate.action")
    public Map<String, Object> deleteTemplate(String id) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            templateService.deleteTemplateById(id);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

}
